﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;

using Microsoft.Win32;
using System.Text;
using System.Windows.Forms;

namespace VehicleTest
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            try
            {
                string connStr = string.Format("path = {0};persist security info=false;packet size=4096",
                    this.Context.Parameters["path"]);

                int i_s = connStr.IndexOf("=");
                int i_e = connStr.IndexOf(";");

                connStr = connStr.Substring(i_s + 1, i_e - i_s - 2).Trim();  //这边减2是为了少一个/,target里面应该最后有/

                CheckOracleClient(connStr + "bin");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


        private static void CheckOracleClient(string path)
        {
            bool find = false;
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Control\Session Manager\Environment", true);
                string str = key.GetValue("path").ToString();
                key.Close();
                string[] str_array = str.Split(';');
                for (int i = 0; i < str_array.GetLength(0); i++)
                {
                    if (str_array[i].ToUpper() == path.ToUpper())
                    {
                        find = true;
                        break;
                    }
                }
                if (!find)
                {
                    str = path + ";" + str;
                    key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Control\Session Manager\Environment", true);
                    key.SetValue("path", str);
                    key.Flush();
                    key.Close();
                }


            }
            catch (Exception ex)
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Control\Session Manager\Environment", true);
                key.CreateSubKey("path");
                key.SetValue("path", path);
                key.Close();
                MessageBox.Show(ex.ToString());
            }

        }
    }
}
